/**
 * 
 */

goog.provide('lscs.weather.WeatherWidget');

goog.require('goog.Uri');
goog.require('goog.net.XhrIo');
goog.require('goog.dom');

lscs.weather.WeatherWidget = function(canvas, location) {
	this.canvas = canvas;
	this.location = location;
	this.weatherUri = new goog.Uri();
	this.weatherUri.setPath('/ajax/weather');
	this.weatherUri.setParameterValue('location', location); 
}

lscs.weather.WeatherWidget.prototype.setLocation = function(location) {
	this.location = location;
	this.weatherUri.setParameterValue('location', location); 
}

lscs.weather.WeatherWidget.prototype.load = function() {
	var self = this;
	goog.net.XhrIo.send(this.weatherUri, function(e) {
	      var xhr = e.target;
	      var weatherDoc = xhr.getResponseXml();
	      
	      try {
	    	  var currentNode = weatherDoc.getElementsByTagName("current_conditions")[0];
		      var condition = currentNode.getElementsByTagName("condition")[0].getAttribute("data");
		      var temp = currentNode.getElementsByTagName("temp_c")[0].getAttribute("data");
		      var humidity = currentNode.getElementsByTagName("humidity")[0].getAttribute("data");
		      var icon = currentNode.getElementsByTagName("icon")[0].getAttribute("data");
		      var wind = currentNode.getElementsByTagName("wind_condition")[0].getAttribute("data");
		      
		      var conditionDom = goog.dom.createDom('label', {'style': 'font-size: 15px; font-weight: bold; display: block;'}, condition);
		      var tempDom = goog.dom.createDom('label', {'style': 'font-size: 30px; display:inline-block; height: 40px; line-height: 40px; vertical-align: top; margin-left: 5px;'}, temp + '\u00B0');
		      var humidityDom = goog.dom.createDom('label', {'style': 'font-size: 10px;'}, humidity);
		      var iconDom = goog.dom.createDom('img', {'style': 'display:inline-block;', 'src': 'http://www.google.com' + icon}, icon);
		      var windDom = goog.dom.createDom('label', {'style': 'font-size: 10px;'}, wind);
		      
		      var container = goog.dom.createDom('div', 'weather-container', iconDom, tempDom, conditionDom, humidityDom);
		      self.canvas.appendChild(container);
		      
		      self.canvas.appendChild(windDom);
	      } catch(error) {
	    	  var errorMsg = goog.dom.createDom('label', null, 'Unable to retrieve weather information');
	    	  self.canvas.appendChild(errorMsg);
	      }				      
	  });
}